package cn.edu.abc.wangzi.ui;
/*
 *  项目名：Wangzi
 *  包名：cn.edu.abc.wangzi.ui
 *  创建者：SKFsky
 *  创建时间：2017/8/27 23:16
 *  描述:   物流查询
 */

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import cn.edu.abc.wangzi.R;
import cn.edu.abc.wangzi.adapter.CourierAdapter;
import cn.edu.abc.wangzi.entity.CourierData;
import cn.edu.abc.wangzi.utils.StaticClass;

public class CourierActivity extends BaseActivity implements View.OnClickListener{

    private EditText et_name;
    private EditText et_number;
    private Button btn_get_courier;
    private ListView mListView;

    private List<CourierData> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courier);

        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_number = (EditText) findViewById(R.id.et_number);
        btn_get_courier = (Button) findViewById(R.id.btn_get_courier);
        btn_get_courier.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.mListView);
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString().trim();
        String number = et_number.getText().toString().trim();
        String url = "http://v.juhe.cn/exp/index?key="+ StaticClass.COURIER_KEY+ "&com=" + name + "&no=" + number;
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(number)){
            RxVolley.get(url, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                    //Toast.makeText(CourierActivity.this, t, Toast.LENGTH_SHORT).show();
                    parsingJson(t);
                }
            });
        }else{
            Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show();
        }
    }

    //解析数据
    private void parsingJson(String t){
        try {
            JSONObject jsonObject = new JSONObject(t);
            JSONObject jsonResult = jsonObject.getJSONObject("result");
            JSONArray jsonArray = jsonResult.getJSONArray("list");
            for (int i=0;i<jsonArray.length();i++){
                JSONObject json = (JSONObject) jsonArray.get(i);

                CourierData data = new CourierData();
                data.setRemark(json.getString("remark"));
                data.setZone(json.getString("zone"));
                data.setDatetime(json.getString("datetime"));
                mList.add(data);
            }
            //倒序  大一java基础里面的
            Collections.reverse(mList);
            CourierAdapter adapter = new CourierAdapter(this,mList);
            mListView.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
